package com.test.daily.leetcode.y2021.m10.day1005;

import java.util.Iterator;

/**
 * licenseKeyFormatting
 *
 * @author Tom on 2021/10/4
 */
public class Algo01_PeekingIterator {

    public static void main(String[] args) {
//        PeekingIterator peekingIterator = new PeekingIterator(new int[]{1,2,3});
    }

    public static class PeekingIterator implements Iterator<Integer> {

        private Iterator<Integer> iterator;
        private Integer nextElement;
//        public PeekingIterator(int[] nums){
//            ArrayList<Integer> a = (ArrayList)Arrays.asList(nums);
//            this.iterator = a.iterator();
//        }

        public PeekingIterator(Iterator<Integer> iterator) {
            // initialize any member here.
            this.iterator = iterator;
            this.nextElement = iterator.next();
        }

        // Returns the next element in the iteration without advancing the iterator.
        public Integer peek() {
            return nextElement;
        }

        // hasNext() and next() should behave the same as in the Iterator interface.
        // Override them if needed.
        @Override
        public Integer next() {
            Integer ret = this.nextElement;
            this.nextElement = this.hasNext()?this.iterator.next():null;
            return ret;
        }

        @Override
        public boolean hasNext() {
            return nextElement != null;
        }
    }

}
